<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use App\Admin\Manager;
class PublicController extends Controller
{
    //展示视图
    public function login()
    {
        return view('admin.public.login');

        //	return view('admin.public.login');
    }
    public function check(Request $request)
    {


        //开始自动验证
        $this->validate($request,[
            'username'  =>  'required|min:2|max:20',
            'password'  =>  'required|min:6',
            'captcha'    =>  'required|size:6|captcha'
        ]);
        $username=$request->input('username');
        $password=$request->input('password');
        //DB::connection()->enableQueryLog();

        $result=Auth::guard('admin')->attempt([
            'username'=>$username,
            'password'=>$password,
            'status'=>'2'
        ]);
//        dd($result);

        if($result){
            //跳转到后台页面
            return redirect('/admin/index/index');
//            return view('/admin/index/index');
//            return json_encode("ddd");
        }else{
            //跳到登陆页面
            return redirect('/admin/public/login')->withErrors([
                'logingError'   =>'用户名或密码错误'
            ]);
        }

    }


    public function logout(Request $request)
    {
        //退出
        Auth::guard('admin')->logout();
        $request->session()->flush();
        $request->session()->regenerate();
        //跳转到登陆界面

        return redirect('/admin/public/login');

    }

    

}
